'use strict';
var fs = require('fs')

/*
  读取
  写入
  管道 pipe
*/

console.log('读取')
var rs = fs.createReadStream('./2/stream/REAMDE.md', 'utf-8')
rs.on('data', function(chunk) {
  console.log('DATA:')
  console.log(chunk)
})
rs.on('end', function() {
  console.log('END')
})
rs.on('error', function(err) {
  console.log('error: ' + err)
})

console.log('写入')
var wr = fs.createWriteStream('./2/stream/wr1.txt', 'utf-8')
wr.write('write line 1\n')
wr.write('write line 2\n')
wr.end()

console.log('写入 Buffer')
var wr2 = fs.createWriteStream('./2/stream/wr2.tar.a')
wr2.write(new Buffer('write buffer line 1 \n'))
wr2.write(new Buffer('write buffer line 2 \n'))
wr2.end()

// 流入
var p1 = fs.createReadStream('./2/stream/smpale.txt')
var p2 = fs.createWriteStream('./2/stream/smpale2.txt')
p1.pipe(p2)
// var p1 = fs.createReadStream('sample.txt');
// var p2 = fs.createWriteStream('copied.txt');
//
// p1.pipe(p2);
